package com.cloud.pole.shop.controller.goods.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.api.advert.AdvertApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.feign.IAdvertService;
import com.cloud.pole.shop.biz.goods.ShopGoodsGroupBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsAdmin;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsGroup;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsGroupRecomm;
import com.cloud.pole.shop.service.goods.GoodsGroupRecommService;
import com.cloud.pole.shop.service.goods.GoodsGroupService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
/*
 * 商品分组
 * */
@Controller
@RequestMapping("/goods/group/admin")
@Slf4j
public class GoodsGroupAdminController extends BaseController<ShopGoodsGroupBiz, ShopGoodsGroup>{

	@Autowired
	private GoodsGroupService goodsGroupService;
	@Autowired
	private GoodsGroupRecommService goodsGroupRecommService;
	
	@ResponseBody
	@GetMapping("allShopGroup")
	public List<ShopGoodsGroup> allShopGroup(){
	 return goodsGroupService.selectAllShopGroup();
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiRestResponse<ShopGoodsGroup> list(Integer pageNo, Integer pageSize) {
		pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
		pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
		try {
			PageInfo<ShopGoodsGroup> pageInfo = goodsGroupService.selectPageShopGoodsGroupAdminList(pageNo, pageSize);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
	@PostMapping("addGoodsGroup")
	@ResponseBody
	public ApiRestResponse<ShopGoodsGroup> addGroup(@RequestBody ShopGoodsGroup shopGoodsGroup){
		String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
		try {
			goodsGroupService.addShopGoodsGroup(token, shopGoodsGroup);
			return  ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	
	}
	
	@GetMapping("getShopsInfo/{id}")
	@ResponseBody
	public ApiRestResponse<ShopGoodsGroup> getShopsInfoById(@PathVariable("id") Integer groupId){
		try {
			ShopGoodsGroup selectById = goodsGroupService.selectById(groupId);
			return   ApiRestResponse.success("200", selectById);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	
	}
	
	@PutMapping("updateGroup")
	@ResponseBody
	public ApiRestResponse<ShopGoodsGroup> updateGroup(@RequestBody ShopGoodsGroup shopGoodsGroup){
		String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
		try {
			goodsGroupService.updateGroup(token, shopGoodsGroup);
			return   ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
		
	}
	
	@DeleteMapping("deleteGroup/{id}")
	@ResponseBody
	public ApiRestResponse<ShopGoodsGroup> deleteGroup(@PathVariable("id") Integer groupId){
		try {
			goodsGroupService.deteGroup(groupId);
			return   ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}	
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/groupAdList")
	@ResponseBody
	public ApiRestResponse<ShopGoodsGroupRecomm> groupList(Integer pageNo, Integer pageSize,Integer shopId) {
		try {
			pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
			pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;		
			PageInfo<ShopGoodsGroupRecomm> pageInfo = goodsGroupRecommService.selectPageShopGoodsGroupAdminList(pageNo, pageSize,shopId);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
	@GetMapping("groupByStatus/{id}")
	@ResponseBody
	public ApiRestResponse<List<ShopGoodsGroup>> groupByStatus(@PathVariable("id") Integer shopId){
		try {
			 List<ShopGoodsGroup> selectByShop = goodsGroupService.selectByShop(shopId);
			return   ApiRestResponse.success("200", selectByShop);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}	
	}
	
	@PutMapping("updateGroupRecomm")
	@ResponseBody
	public ApiRestResponse<ShopGoodsGroupRecomm> updateGroupRecomm(@RequestBody ShopGoodsGroupRecomm shopGoodsGroupRecomm){
		try {
			ShopGoodsGroupRecomm selectByTerm = goodsGroupRecommService.selectByTerm(shopGoodsGroupRecomm);
			goodsGroupRecommService.updateGroupRecomm(shopGoodsGroupRecomm);
			return   ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
		
	}
}
